package com.test.demo.controller;



import com.test.demo.commons.ResponseResult;
import com.test.demo.domain.Messages;
import com.test.demo.service.MessagesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;


/**
 * 后台文章内容管理
 *
 * @author 远 chards_
 * @date: 2020-02-22 02:05
 */

@RestController
@RequestMapping(value = "/message/back")
@CrossOrigin(origins = "*", maxAge = 3600,allowCredentials = "true")
public class MessageController {



    @Autowired
    public MessagesService messagesService;

    /**
     * 文章内容添加
     * @param
     * @return
     */
    @PostMapping(value = "/insert")
    public ResponseResult<Messages> info(@RequestBody Messages message){
        System.out.println(message);
           if (message==null||message.getAuthor()==null ||message.getSource()==null||message.getType()==null||message.getTitle()==null){
               return new ResponseResult<Messages>(ResponseResult.CodeStatus.FAIL,"发布的信息中有字段不能为空");
           }
        message.setDate(new Date());
            int result = messagesService.insert(message);
        //注册成功
        if (result>0){
            return new ResponseResult<Messages>(ResponseResult.CodeStatus.OK,"内容添加成功",message);
        }
        return new ResponseResult<Messages>(ResponseResult.CodeStatus.FAIL,"内容添加失败");
 }

    /**
     * 查询所有文章内容
     * @param
     * @return
     */
    @GetMapping(value = "/all")
    public ResponseResult<List> info(){
        return new ResponseResult<List>(ResponseResult.CodeStatus.OK,"内容显示",messagesService.selectAll());
    }

    /**
     * 根据id删除文章内容
     * @param
     * @return
     */
    @PostMapping(value = "/delete")
    public ResponseResult<Void> deleteId(@RequestBody Messages message){

        int result = messagesService.delete(message.getId());
        if (result>0){
            return new ResponseResult<Void>(ResponseResult.CodeStatus.OK,"删除成功");
        }
        return new ResponseResult<Void>(ResponseResult.CodeStatus.FAIL,"删除失败");
    }


}
